package com.lrkj.common.core.service;

import java.util.ArrayList;
import java.util.List;

public interface BaseService<T> {


    public int batchAddData(List<T> saveBeans);


    default int doSaveList(List<T> batchBean) {
        List<T> saveBeans = new ArrayList<>();
        //数据同步单次新增条数
        Integer maxNum = 500;
        if (batchBean.size() > maxNum) {
            for (int i = 0; i < batchBean.size(); i++) {
                if ((i + 1) % maxNum == 0) {
                    saveBeans.add(batchBean.get(i));
                    batchAddData(saveBeans);
                    saveBeans.clear();
                } else {
                    saveBeans.add(batchBean.get(i));
                }
            }
            if (null != saveBeans && saveBeans.size() > 0) {
                batchAddData(saveBeans);
            }
        } else {
            if (batchBean.size() > 0) {
                batchAddData(batchBean);
            }
        }
        return 1;
    }

    default int doSaveList(List<T> batchBean, int maxNum) {
        List<T> saveBeans = new ArrayList<>();
        //数据同步单次新增条数
        if (batchBean.size() > maxNum) {
            for (int i = 0; i < batchBean.size(); i++) {
                if ((i + 1) % maxNum == 0) {
                    saveBeans.add(batchBean.get(i));
                    batchAddData(saveBeans);
                    saveBeans.clear();
                } else {
                    saveBeans.add(batchBean.get(i));
                }
            }
            if (null != saveBeans && saveBeans.size() > 0) {
                batchAddData(saveBeans);
            }
        } else {
            if (batchBean.size() > 0) {
                batchAddData(batchBean);
            }
        }
        return 1;
    }

}
